﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using entgpguayas.Code;
using Subgurim.Controles;
using ENT.GP.Core;
using ENT.GP.Core.Core;
using ENT.GP.Core.Proyecto;
using Ext.Net;

public partial class wucMapas : ControlesUsuario
{
    protected void Page_Load(object sender, EventArgs e)
    {
        UbicarProyectos();
    }

    void UbicarProyectos()
    {
        try
        {
            GMap1.Width = Window1.Width;
            GMap1.Height = Window1.Height;
            var listaProyectos = ProyectoLista.GetProyectoLista( ProyectoCriterio.NewProyectoCriterio());
            foreach (var proyecto in listaProyectos)
            {

                GMap1.addGMapUI(new GMapUI());
                GMap1.enableGKeyboardHandler = true;


                GLatLng latlong = new GLatLng(proyecto.Latitud, proyecto.Longitud);
                GMapType.GTypes maptype = GMapType.GTypes.Hybrid;
                GMap1.setCenter(latlong, 9, maptype);
                GMarker icono = new GMarker(latlong);

                GInfoWindowTabs InfoTabs = new GInfoWindowTabs();
                InfoTabs.point = latlong;
                InfoTabs.gMarker = icono;
                System.Collections.Generic.List<GInfoWindowTab> tabs = new System.Collections.Generic.List<GInfoWindowTab>();
                
                tabs.Add(new GInfoWindowTab("Descripción", proyecto.Descripcion));
                tabs.Add(new GInfoWindowTab("Costo", proyecto.Presupuesto.ToString()));
                tabs.Add(new GInfoWindowTab("Población", FormarHtmlPA(proyecto.PoblacionAfectada, proyecto.NumeroHabitantes)));
                var giwo = new GInfoWindowOptions();
                giwo.maxWidth = 300;

                InfoTabs.options = giwo;
                InfoTabs.tabs = tabs;
                GMap1.addInfoWindowTabs(InfoTabs);

                GMap1.addControl(new GControl(GControl.extraBuilt.MarkCenter));
            }
        }
        catch (Exception ex)
        {
            Util.RegistarLog(ex.ToString());
        }
    }

    protected void CambiaTamano(object sender, DirectEventArgs e)
    {
        GMap1.Width = Window1.Width;
        GMap1.Height = Window1.Height;
    }

    string FormarHtmlPA(int idPA, double numeroHabitantes)
    {
        string PA = "", res = "";
        switch (idPA)
        {
            case 605:
                PA = "Urbano";
                break;
            case 606:
                PA = "Urbano Marginal";
                break;
            case 607:
                PA = "Rural";
                break;
        }

        res = string.Format("<div style=\"font-family:Tahoma, Geneva, sans-serif;\"><p>La poblaci&oacute;n afectada es: <font style=\"font-weight:bold;\">{0}</font></p><p>El n&uacute;mero de habitantes beneficiados es de: <font style=\"font-weight:bold;\">{1}</font></p></div>", PA, Convert.ToInt32(numeroHabitantes));
        return res;
    }

    protected string GMap1_Click(object s, GAjaxServerEventArgs e)
    {
        GLatLng latlong = new GLatLng(e.point.lat, e.point.lng);
        GMap1.setCenter(latlong,4);

        GMarker icono = new GMarker(latlong);
        String strInfo = "hola christmo";
        GInfoWindow window = new GInfoWindow(icono, strInfo, false, GListener.Event.click);

        GMap1.addGMarker(icono);
        GMap1.addInfoWindow(window);


        return string.Format("{0}.panTo({1})", e.who, e.point.ToString("new"));
    }

    private void Buscar()
    {
        GMap1.GZoom = 9;
        GMap1.addControl(new GControl(GControl.preBuilt.LargeMapControl));

        GeoCoding geoCoding = new GeoCoding();

        geoCoding.show = GeoCoding.ShowEnum.infowindow;
        geoCoding.openedOnLoad = true;
        geoCoding.errorText = "No está";
        geoCoding.buttonText = "Buscar";
        geoCoding.infoText = "Buscar GeoCode";

        GMap1.addGeoCode(geoCoding);
    }

    public void Buscar(String strCiudad)
    {
        //--set Icon
        //GIcon icon = new GIcon();
        //icon.image = "icons/icon.png";
        //icon.shadow = "icons/sombra.png";
        //icon.iconSize = new GSize(32, 32);
        //icon.shadowSize = new GSize(56, 32);
        //icon.iconAnchor = new GPoint(16, 32);
        //icon.infoWindowAnchor = new GPoint(16, 0);

        //GMarkerOptions mOpts = new GMarkerOptions();
        //mOpts.icon = icon;
        //--

        string Key = System.Configuration.ConfigurationManager.AppSettings.Get("googlemaps.subgurim.net");
        GeoCode geocode = GMap.geoCodeRequest(strCiudad + ",Ecuador", Key);

        GMap1.addControl(new GControl(GControl.preBuilt.LargeMapControl));

        GLatLng latlong = new GLatLng(geocode.Placemark.coordinates.lat, geocode.Placemark.coordinates.lng);

        //GMap1.setCenter(latlong,12);

//        GMarker icono = new GMarker(latlong, mOpts);
        GMarker icono = new GMarker(latlong);
        String strInfo = "Provincia: <b>" + geocode.Placemark.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName + "</b><br /> " +
                         "Canton: <b>" + geocode.Placemark.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName + "</b><br /> " +
                         "Pais: <b>" + geocode.Placemark.AddressDetails.Country.CountryNameCode + "</b><br /> " +
                         "<div>UTPL Loja:<br /><img src=\"http://marysmk.files.wordpress.com/2007/06/utpl1.jpg\" alt=\"christmo\" /></div>";
        GInfoWindow window = new GInfoWindow(icono, strInfo, false, GListener.Event.click);

        //GMap1.addGMarker(icono);
        GMap1.addInfoWindow(window);
    }
}

